package com.hualude.roadsign.repository;


import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.SignfaultEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by xu-jiaHui on 2017/2/28.
 */
public interface SignfaultRepository extends CrudRepository<SignfaultEntity, Long> {
    /**
     * @Description: 获取用于DT显示故障上报信息
     * @Author: xu-jiaHui
     * @Date: 2017/2/28 19:33
     */
    public List<SignfaultDto> getSignfault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 获取用于DT显示的故障上报数量
     * @Author: xu-jiaHui
     * @Date: 2017/2/28 19:40
     */
    public Integer getCount4DT(Integer actorId, Integer userId, Integer orgId, String strSearch) throws Exception;

    public Integer getFaultUnAuditCount(Integer actorId, Integer userId) throws Exception;

    public Integer getUnCount(Integer actorId, Integer userId, Integer orgId) throws Exception;


    /**
     * @Description: 获取报修审批的故障信息用于DT
     * @Author: xu-jiaHui
     * @Date: 2017/3/30 19:01
     */
    public List<SignfaultDto> getSignfault4Audit(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    public Integer getFaultAuditCount(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception;

    /**
     * @Description: 删除故障上报信息
     * @Author: xu-jiaHui
     * @Date: 2017/3/2 15:44
     */
    @Modifying
    @Query(value = "delete from SignfaultEntity s where s.signFaultId=?1")
    public Integer deleteSignFaultById(Integer signFaultId) throws Exception;

    /**
     * @Description: 根据id更新故障上报信息
     * @Author: xu-jiaHui
     * @Date: 2017/3/5 10:34
     */
    @Modifying
    @Query(value = "update SignfaultEntity s set s.signFaultLevel=?2,s.signFaultType=?3,s.signFaultPic=?4,s.signFaultReason=?5" +
            ",s.repairAmount=?6 where s.signFaultId=?1")
    public Integer updateSignFaultById(Integer signFaultId, Integer signFaultLevel, Integer signFaultType, String signFaultPic,
                                       String signFaultReason, Double repairAmount) throws Exception;

    /**
     * @Description: 根据路标Id获取经纬度
     * @Author: xu-jiaHui
     * @Date: 2017/3/6 19:14
     */
    public List<RoadpoleDto> getlongLatitude4Map(Integer roadSignId) throws Exception;

    /**
     * @Description: 故障审核
     * @Author: xu-jiaHui
     * @Date: 2017/3/15 11:09
     */
    @Modifying
    @Query(value = "update SignfaultEntity s set s.mainCheck=?1,s.signFaultCheckBack=?2,s.mainUserId=?4 where s.signFaultId=?3")
    public Integer signFaultAuditByMain(Integer isPass, String signFaultCheckBack, Integer signFaultId, Integer userId) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity s set s.companyCheck=?1,s.signFaultCheckBack=?2 ,s.companyUserId=?4,s.maintenancePeopleId=?5 where s.signFaultId=?3")
    public Integer signFaultAudiByCompany(Integer isPass, String signFaultCheckBack, Integer signFaultId, Integer signFaultPeopleId, Integer maintenancePeopleId) throws Exception;

    /**
     * @Description: 获取未审核记录
     * @Author: pan-jianan
     * @Date: 2017/4/19 19:57
     */
    public List<SignFaultReportDto> getUnauditedFault(Integer orgId, String actorIds, Integer count, Integer size)
            throws Exception;



    /**
     * @Description: 获取未通过的记录
     * @Author: pan-jianan
     * @Date: 2017/4/19 19:57
     */

    public List<SignFaultReportDto> getUnpassFault(Integer orgId,
                                                   String actorIds,
                                                   Integer count,
                                                   Integer size) throws Exception;


    /**
     * @Description: 获取已通过的记录
     * @Author: pan-jianan
     * @Date: 2017/4/19 19:57
     */

    public List<SignFaultReportDto> getPassFault(Integer orgId,
                                                 String actorIds,
                                                 Integer count,
                                                 Integer size) throws Exception;

    @Modifying
    @Query(value = " FROM SignfaultEntity WHERE roadSignId =?1")
    public List<SignfaultEntity> getSignfaultInfoByRoadSignId(Integer roadSignId) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity set companyCheck=?2,signFaultCheckBack=?3,signCheckTime=?4,companyUserId=?5 where signFaultId=?1")
    public Integer updateSignFault2failByCompany(Integer signFaultId, Integer status, String signFaultCheckBack, String dateNowStr, Integer userId) throws Exception;

    @Query(value = "SELECT companyCheck FROM SignfaultEntity WHERE signFaultId =?1")
    public Integer judgeCheck(Integer signFaultId) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity set mainCheck=?2,signFaultCheckBack=?3,signCheckTime=?4,mainUserId=?5 where signFaultId=?1")
    public Integer updateSignFault2failByMain(Integer signFaultId, Integer status, String signFaultCheckBack, String dateNowStr, Integer userId) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity set mainCheck=?2,signFaultCheckBack=?3,signCheckTime=?4,mainUserId=?5 where signFaultId=?1")
    Integer updateSignFault2successByMain(Integer signFaultId, Integer status, String signFaultCheckBack, String dateNowStr, Integer userId);

    @Modifying
    @Query(value = "update SignfaultEntity set companyCheck=?2,signFaultCheckBack=?3,signCheckTime=?4,companyUserId=?5 where signFaultId=?1")
    Integer updateSignFault2successByCompany(Integer signFaultId, Integer status, String signFaultCheckBack, String dateNowStr, Integer userId);

    public List<SignFaultReportDto> getUnauditedFaultByUserId(Integer userId, String actorIds, Integer count, Integer size) throws Exception;

    //public List<SignFaultReportDto> getAuditedFaultByuserId(Integer userId, Integer flagAdmin) throws Exception;

    //public List<SignFaultReportDto> getAllFaultByuseId(Integer userId) throws Exception;

    public List<SignFaultReportDto> getPassFaultByuserId(Integer userId, String actorIds, Integer count, Integer size) throws Exception;

    public List<SignFaultReportDto> getUnpassFaultByuserId(Integer userId, String actorIds, Integer count, Integer size) throws Exception;

    /**
     * @Description: 获取当前故障信息用于Dt
     * @Author: xu-jiaHui
     * @Date: 2017/3/18 14:58
     */
    public List<SignfaultDto> getCurrentFault4DT(DTRequestDto dtRequestDto, Integer actorId, String maintenancePeopleName) throws Exception;

    public Integer getCurrentFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, String maintenancePeopleName) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity s set s.signCheckTime=?1 where s.signFaultId=?2")
    public Integer updateSignCheckTime(String signCheckTime, Integer signFaultId) throws Exception;

    @Query(value = "select new com.hualude.roadsign.dto.SignfaultDto(s.signFaultId,s.roadSignId, s.signFaultLevel, " +
            "s.signFaultPic,s.signFaultType,s.signFaultReason, s.signFaultTime,s.companyUserId, s.mainUserId,s.signFaultCheckBack,s.repairAmount) " +
            "from SignfaultEntity s WHERE s.signFaultId=?1 ")
    public List<SignfaultDto> getSignFaultINfo4addBySignFaultId(Integer signFaultId) throws Exception;

    /**
     * @Description: 获取驳回故障信息
     * @Author: xu-jiaHui
     * @Date: 2017/3/31 21:58
     */
    public List<SignfaultDto> getRejectedFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    public Integer getRejectedFaultCount4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId) throws Exception;

    /**
     * @Description: 撤销驳回信息
     * @Author: xu-jiaHui
     * @Date: 2017/4/1 21:18
     */
    @Modifying
    @Query(value = "update SignfaultEntity s set s.companyCheck=?1  ,s.signCheckTime=?3 where s.signFaultId=?2")
    public Integer updateSignFaultCompanyCheckById(Integer isPass, Integer signFaultId, String signCheckTime) throws Exception;

    @Modifying
    @Query(value = "update SignfaultEntity s set s.mainCheck=?1 , s.signCheckTime=?3 where s.signFaultId=?2")
    public Integer updateSignFaultMainCheckById(Integer isPass, Integer signFaultId, String signCheckTime) throws Exception;

    @Query(value = "select new com.hualude.roadsign.dto.UserOrgDto4Notice(o.orgId,o.orgName,u.userId,u.userName)" +
            " from UserEntity u,UserActorEntity ua,OrgEntity o" +
            " where ua.userId=u.userId and o.orgId=u.orgId and ua.actorId=5 and o.orgId=?1")
    public List<UserOrgDto4Notice> getUserInfo2ReportFaultByOrg(Integer orgId) throws Exception;

    /**
     * @Description: 根据标牌Id删除故障记录
     * @Author: shao-yikun
     * @Date: 2017/4/10 19:12
     */
    @Modifying
    @Query(value = "delete from SignfaultEntity where roadSignId = ?1")
    public Integer deleteSignfaultByRoadSignId(Integer roadSignId) throws Exception;

    /**
     * @Description: 通过id获取审核拒绝原因
     * @Author: xu-jiaHui
     * @Date: 2017/5/7 2:22
     */
    public String getCheckBackBySignFaultId(Integer roadSignId) throws Exception;

    /**
     * @Description: 通过roadSignId获取SignFault表中最新的故障信息
     * @Author: xu-jiaHui
     * @Date: 2017/5/16 21:25
     */
    public List<SignfaultDto> getCheckByRoadSignId(Integer roadSignId) throws Exception;

    /**
     * @Description: 通过signFaultId获取故障审核情况
     * @Author: jiang-weirong
     * @Date: 2017/5/18 21:48
     */
    @Modifying
    @Query(value = "select new com.hualude.roadsign.dto.SignfaultDto(s.companyCheck,s.mainCheck) " +
            "from SignfaultEntity s WHERE s.signFaultId=?1 ")
    public List<SignfaultDto> getSignFaultCheck(Integer signFaultId) throws Exception;

    /**
     * @Description: 获取标牌的故障编号
     * @Author: jiang-weirong
     * @Date: 2017/5/18 21:48
     */
    @Modifying
    @Query(value = "select s.signFaultId AS signFaultId " +
            "from SignfaultEntity s WHERE s.roadSignId=?1 ORDER BY s.signFaultTime DESC ")
    public List<Integer> getNewSignFaultId(Integer roadSignId) throws Exception;

    /**
     * @Description: 根据故障Id获取故障信息
     * @Author: shao-yikun
     * @Date: 2017/5/21 1:31
     */
    @Query(value = "from SignfaultEntity WHERE signFaultId = ?1")
    public SignfaultEntity getSignfaultBySignFaultId(Integer signFaultId) throws Exception;
}
